package com.wyp168.juc;

/**
 * @ClassName Synchronized
 * @Description TODO
 * @Author wyp168
 * @Date 2022/7/8 09:09
 */
public class SynchronizedDemo {
    public static volatile Object resourceA = new Object();

    public static void main(String[] args) {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                System.out.println("ThreadA try to get resourceA lock");
                synchronized (resourceA) {
                    System.out.println("ThreadA has gotten the resourceA lock");
                    //调用共享变量的wait()方法，来注册threadA线程
                    try {
                        resourceA.wait(1000,100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("ThreadA has finished...");
                    long endTime = System.currentTimeMillis();
                    System.out.println("total consume " + (endTime - startTime) + " Millis");
                }
            }
        });

        //启动ThreadA 线程
        threadA.start();

        //调用threadA的interrupt()方法关闭线程
//        threadA.interrupt();
    }
}
